package com.yds.ydsdatasetbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yds.ydsdatasetbackend.constant.UserConstant;
import com.yds.ydsdatasetbackend.exception.BusinessException;
import com.yds.ydsdatasetbackend.exception.ErrorCode;
import com.yds.ydsdatasetbackend.exception.ThrowUtils;
import com.yds.ydsdatasetbackend.model.dto.file.variables.FileVariablesQueryRequest;
import com.yds.ydsdatasetbackend.model.entity.File;
import com.yds.ydsdatasetbackend.model.entity.FileVariables;
import com.yds.ydsdatasetbackend.model.entity.User;
import com.yds.ydsdatasetbackend.model.vo.FileVO;
import com.yds.ydsdatasetbackend.model.vo.FileVariablesVO;
import com.yds.ydsdatasetbackend.service.FileService;
import com.yds.ydsdatasetbackend.service.FileVariablesService;
import com.yds.ydsdatasetbackend.mapper.FileVariablesMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author YDS
* @description 针对表【file_variables(数据变量表)】的数据库操作Service实现
* @createDate 2025-01-31 12:22:59
*/
@Service
public class FileVariablesServiceImpl extends ServiceImpl<FileVariablesMapper, FileVariables>
    implements FileVariablesService{

    @Resource
    @Lazy
    private FileService fileService;

    @Override
    public void validFileVariable(FileVariables fileVariables, User loginUser) {
        ThrowUtils.throwIf(fileVariables == null, ErrorCode.PARAMS_ERROR);

        // 用户角色校验

        Long fileId = fileVariables.getFileId();
        File file = fileService.getById(fileId);
        if(!file.getUserId().equals(loginUser.getId()) && !loginUser.getUserRole().equals(UserConstant.ADMIN_ROLE)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR,"权限不足");
        }

        // 从对象中取值
        Long id = fileVariables.getId();
        String name = fileVariables.getName();
        String role = fileVariables.getRole();
        String type = fileVariables.getType();
        String description = fileVariables.getDescription();
        String units = fileVariables.getUnits();
        String missing = fileVariables.getMissing();

        // 修改数据时，id 不能为空，有参数则校验
        ThrowUtils.throwIf(ObjUtil.isNull(id), ErrorCode.PARAMS_ERROR, "id 不能为空");
        if (StrUtil.isNotBlank(name)) {
            ThrowUtils.throwIf(name.length() > 20, ErrorCode.PARAMS_ERROR, "名称 过长");
        }
        ThrowUtils.throwIf(description.length() > 200, ErrorCode.PARAMS_ERROR, "描述过长");
        ThrowUtils.throwIf(role.length() > 10, ErrorCode.PARAMS_ERROR, "角色名称过长");
        ThrowUtils.throwIf(units.length() > 10, ErrorCode.PARAMS_ERROR, "单位名称过长");
        ThrowUtils.throwIf(type.length() > 10, ErrorCode.PARAMS_ERROR, "类型名称过长");
        ThrowUtils.throwIf(!missing.equals("no") && !missing.equals("yes"), ErrorCode.PARAMS_ERROR, "缺失值填写错误");
    }


    /**
     * 获取脱敏文件变量信息
     *
     * @param fileVariables
     * @return
     */
    @Override
    public FileVariablesVO getFileVariablesVO(FileVariables fileVariables) {
        if (fileVariables == null) {
            return null;
        }
        FileVariablesVO fileVariablesVO = new FileVariablesVO();
        BeanUtil.copyProperties(fileVariables, fileVariablesVO);
        return fileVariablesVO;
    }

    /**
     * 获取脱敏后的变量列表
     *
     * @param fileVariablesList
     * @return
     */
    @Override
    public List<FileVariablesVO> getFileVariablesVOList(List<FileVariables> fileVariablesList) {
        if (CollUtil.isEmpty(fileVariablesList)) {
            return new ArrayList<>();
        }
        return fileVariablesList.stream()
                .map(this::getFileVariablesVO)
                .collect(Collectors.toList());
    }

    @Override
    public QueryWrapper<FileVariables> getQueryWrapper(FileVariablesQueryRequest fileVariablesQueryRequest) {
        if (fileVariablesQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = fileVariablesQueryRequest.getId();
        Long fileId = fileVariablesQueryRequest.getFileId();
        Long columnNumber = fileVariablesQueryRequest.getColumnNumber();
        String name = fileVariablesQueryRequest.getName();
        String role = fileVariablesQueryRequest.getRole();
        String type = fileVariablesQueryRequest.getType();
        String description = fileVariablesQueryRequest.getDescription();
        String units = fileVariablesQueryRequest.getUnits();
        String missing = fileVariablesQueryRequest.getMissing();
        String sortField = fileVariablesQueryRequest.getSortField();
        String sortOrder = fileVariablesQueryRequest.getSortOrder();

        QueryWrapper<FileVariables> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotNull(fileId), "fileId", fileId);
        queryWrapper.eq(ObjUtil.isNotNull(columnNumber), "columnNumber", columnNumber);
        queryWrapper.eq(StrUtil.isNotBlank(role), "role", role);
        queryWrapper.eq(StrUtil.isNotBlank(type), "type", type);
        queryWrapper.eq(StrUtil.isNotBlank(units), "units", units);
        queryWrapper.eq(StrUtil.isNotBlank(missing), "missing", missing);
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        queryWrapper.like(StrUtil.isNotBlank(description), "description", description);
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }



}




